package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.domain.CheckGroup;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.service.CheckGroupService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service(interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;



    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //获取参数对象中的具体参数
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        //调用分页插件的方法
        PageHelper.startPage(currentPage, pageSize);

        //调用业务层分页查询方法
        Page<CheckGroup> page = checkGroupDao.selectByCondition(queryString);

        //拆分得到的结果,封装发送到controller层的数据
        return new PageResult(page.getTotal(), page.getResult());



    }

    @Override
    public void addCheckGroup(Integer[] checkItemIds, CheckGroup checkGroup) {
        //添加检查组基本信息
        checkGroupDao.addCheckGroupInfo(checkGroup);

        //添加检查组关联的检查项的信息
        setCheckGroupAndCheckItem(checkGroup.getId(), checkItemIds);
    }


    private void setCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkItemIds) {
        if (checkItemIds != null && checkItemIds.length > 0) {
            for (Integer checkItemId : checkItemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkGroup_id", checkGroupId);
                map.put("checkItem_id", checkItemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }

    @Override
    public void deleteCheckGroup(Integer id) {
        checkGroupDao.deleteCheckItemsByGroupId(id);
        checkGroupDao.deleteCheckGroupById(id);
    }

    @Override
    public CheckGroup findCheckGroupInfo(Integer checkGroupId) {
        return checkGroupDao.findCheckGroupInfoById(checkGroupId);
    }

    @Override
    public List<Integer> findAllCheckItemByGid(Integer checkGroupId) {
        return checkGroupDao.findAllCheckItemByGid(checkGroupId);
    }

    @Override
    public void edit(Integer[] checkItemIds, CheckGroup checkGroup) {
        //删除已有的关系
        checkGroupDao.deleteCheckItemsByGroupId(checkGroup.getId());
        checkGroupDao.deleteCheckGroupById(checkGroup.getId());

        //更新数据
        checkGroupDao.addCheckGroupInfo(checkGroup);
        setCheckGroupAndCheckItem(checkGroup.getId(), checkItemIds);
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }
}
